﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SmartRFIDSystem19.model
{
    public class Mail
    {
        // 发送人服务器
        const string smtpServer = "smtp.exmail.qq.com";
        //const string smtpServer = "smtp.yeah.net";
        //发件人服务器端口号
        // const int port = 465;
        const int port = 25;
        // 发件人邮箱地址
        const string senderEmail = "skygod@skygodnet.cn";
        //const string senderEmail = "zimeiot2202@yeah.net";
        // 发件人邮箱密码
        const string senderPassward = "NaUSguBE6Ydm5aaX";
        //const string senderPassward = "WQWRGUGBNUAFOEIL";
        //邮件主题
        static string subject = "智慧一卡通系统验证码";
        //邮件内容
        static string body = "验证码是：";

        //发送邮件函数
        public static void sendMail(string addTo, string subject, string body)
        {
            // 创建 SmtpClient 实例
            var client = new SmtpClient(smtpServer, port); // 设置 SMTP 服务器和端口

            // 配置 SmtpClient
            client.EnableSsl = true; //启用SSL
            client.Credentials = new NetworkCredential(senderEmail, senderPassward); // 设置发件邮箱地址和密码

            try
            {
                // 创建 MailMessage 实例
                var message = new MailMessage(senderEmail, addTo, subject, body);

                // 发送邮件
                client.Send(message);
                //弹出消息框
                MessageBox.Show("邮件发送成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show("发送邮件时出现错误: " + ex.Message);
            }
        }
        public static void sendMail(string addTo)
        {
            sendMail(addTo, subject, body);
        }
        //校验邮箱地址是否合法
        public static bool checkEmail(string email)
        {
            try
            {
                MailAddress mail = new MailAddress(email);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
